package com.nangle.managehost.adapter;

import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.nangle.managehost.R;
import com.nangle.managehost.entity.BedMessage;
import com.nangle.managehost.entity.Doctor;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2016/9/1 16:43
 * 描述：
 */
public class NurseAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<Doctor> mDatas;

    public NurseAdapter(Context context, List<Doctor> mDatas) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.nursecard_layout, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.doctorName = (TextView) convertView.findViewById(R.id.doctor_name);
            viewHolder.doctorAge = (TextView) convertView.findViewById(R.id.doctor_age);
            viewHolder.doctorTitle = (TextView) convertView.findViewById(R.id.nurse_position);
            viewHolder.llyBedList = (LinearLayout) convertView.findViewById(R.id.lly_bed_list);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Doctor doctor = mDatas.get(position);

        viewHolder.doctorName.setText(doctor.getName());
        viewHolder.doctorAge.setText(""+doctor.getAge());
        viewHolder.doctorTitle.setText(Html.fromHtml(doctor.getTitle()));
        List<BedMessage> bedMessages = doctor.getBedMessages();
        int count = 0;
        if(bedMessages!=null) {
            count = bedMessages.size();
        }
        viewHolder.llyBedList.removeAllViews();
        for(int i= 0;i<count;i++){
            getBedMessage(viewHolder.llyBedList,bedMessages.get(i).getBedNumber());
        }
        return convertView;
    }

    private final class ViewHolder {
        private TextView doctorName;
        private TextView doctorAge;
        private TextView doctorTitle;
        private LinearLayout llyBedList;
    }

    private void getBedMessage(LinearLayout mLinearLayout,int msg){

        TextView mTextView = new TextView(mContext);
        mTextView.setText(msg+"床");
        mTextView.setTextColor(mContext.getResources().getColor(R.color.green));
        mTextView.setBackgroundResource(R.drawable.circle_green);
        mTextView.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mLayoutParams.setMargins(3,3,3,3);
        mLinearLayout.addView(mTextView, mLayoutParams);
    }
}